﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Globalization;

namespace ClientApplication
{
    [DataContract]
    [Serializable]
    public class Contest
    {
        public string id { get; set; }
        [DataMember]
        public Rules rules { get; set; }
        [DataMember]
        public ContestantList contestantList { get;  set; }
        [DataMember]
        public List<Judge> allJudges { get;  set; }
        public int activeDiver { get;  set; }
        public int activeJump { get;  set; }
        public string activeJumpCode { get;  set; }
        public List<Score> activeJumpScoreList { get;  set; }
        //bools for the server to keep track of where in the contest setup the program are.
        public bool acceptingScore;
        public bool rulesSet;
        public bool contestantsSet;
        public bool started;
  
        public Contest()
        {
            id = "";
            started = false;
            rulesSet = false;
            contestantsSet = false;
            activeDiver = 0;
            activeJump = 0;
            this.rules = new Rules();
            this.contestantList = new ContestantList();
            this.allJudges = new List<Judge>();
            this.activeJumpScoreList = new List<Score>();
            this.acceptingScore = false;
        }
        public Contest(string id)
        {
            started = false;
            this.id = id;
            rulesSet = false;
            contestantsSet = false;
            activeDiver = 0;
            activeJump = 0;
            this.rules = new Rules();
            this.contestantList = new ContestantList();
            this.allJudges = new List<Judge>();
            this.activeJumpScoreList = new List<Score>();
            this.acceptingScore = false;
        }
        public Contest(int date, string place, string id)
        {
            rulesSet = false;
            contestantsSet = false;
            this.id = id;
            activeDiver = 0;
            activeJump = 0;
            this.contestantList = new ContestantList();
            this.allJudges = new List<Judge>();
            this.activeJumpScoreList = new List<Score>();
            this.acceptingScore = false;

        }
        public int incrementActiveDiver()
        {
            //Checks if every diver is done with current round if not, increment active diver.
            if(rules.sex == 1 && activeJump == 4 && activeDiver == contestantList.contestants.Count - 1)
                return 1;
            else if(rules.sex == 0 && activeJump == 5 && activeDiver == contestantList.contestants.Count - 1)
                return 1; 

            
            if (activeDiver == contestantList.contestants.Count - 1)
            {
                activeJump++;
                activeDiver = 0;
            }
            else
            {
                activeDiver++;
            }
            return 0;
        }


        public Contestant GetActiveContestant()
        {
            return contestantList[activeDiver];
        }

        public void SetUpRules(Rules rules)
        {
            this.rules = rules;
            rulesSet = true;
        }

        public void PresentResult()
        {
            contestantList.SortList();
            contestantList.PrintList();
            
        }
    }
}
